#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node
{
int data;
struct node *next;
}*p,*q;
int a=0;

int length()
{
int ctr;
struct node *temp;
temp=p;
while(temp!=NULL)
{
temp=temp->next;
ctr++;
}
return ctr;
}


insert(int num, int pos)
{
a++;
int i;
struct node *temp,*t,*r;
r=p;
if (pos == 1)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
if ( p== NULL)
{
p=temp;
p->next=NULL;
}
else
{
temp->next=p;
p=temp;
}
return 0;
}
else
{
for(i=1;i<pos;i++)
{
t=r;   
r=r->next;
}
temp=malloc(sizeof(struct node));
temp->data=num;
t->next=temp;
t=temp;
t->next=r;
return 0;
}
}


int display(struct node *r)
{
r=p;
if(r==NULL)
{
printf("NO ELEMENT IN THE LIST :");
return 0;
}
while(r!=NULL)
{
printf(" -> %d ",r->data);
r=r->next;
}
printf("");
}

int delnode(int num)
{
struct node *temp, *m;
temp=p;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==p)
{
p=temp->next;
free(temp);
return;
}
else
{
m->next=temp->next;
free(temp);
return;
}
}
else
{
m=temp;
temp= temp->next;
}
}
printf("ELEMENT %d NOT FOUND ", num);
}

void append(int num)
{   
struct node *temp=malloc(sizeof(struct node)),*i,*j;
if(temp==NULL)
printf("\n\tMemory allocation error");
else
{ 
temp->data=num;
temp->next=NULL;
}
if(p==NULL)
p=temp;
else
{   
for(i=p;i!=NULL;i=i->next)
j=i;
j->next=temp;
}   
}

int count(num)
{   
struct node *i;
int ctr=0;
for(i=p;i!=NULL;i=i->next)
{   
if(i->data==num)
ctr++;
}
return ctr;
}

int index11(int num)
{   
struct node *i;
int indx=0,flag=0;
for(i=p;i!=NULL;i=i->next)
{   
if(i->data==num)
{   
flag=1;
break;
}
indx++;
}
if(flag==0)
printf("\n\tValue not found");
else
return indx;
}

void reverse(struct node *q)
{
struct node *a, *b,*s;
a=q;
b=NULL;
while(a!=NULL)
{
s=b;
b=a;
a=a->next;
b->next=s;
}
p=b;
}

void main()
{
int i;
p=NULL;
while(1)
{
printf("1.INSERT A NUMBER AT PARTICULAR LOCATION:\n");
printf("2.DISPLAY THE ELEMENTS OF LIST:\n");
printf("3.DELETE THE ELEMENT FROM THE LIST:\n");
printf("4.APPEND AN ELEMENT:\n");
printf("5.COUNT THE NUMBER OF PARTICULAR ELEMENT IN THE LIST:\n");
printf("6.INDEX OF THE ELEMENT:\n");
printf("7.COUNT THE NUMBER OF ELEMENTS IN THE LIST:\n");
printf("8.EXTEND THE LIST:\n");
printf("9.REVERSE THE LIST:\n");
printf("0.GET OUT OF LINKED LIST\n :");
printf("PLEASE, ENTER THE NUMBER:\n");
scanf("%d",&i);

switch(i)
{
case 1:
{
int num ,pos;
printf("PLEASE ENTER THE NUMBER :");
scanf("%d",&num);
printf("PLEASE ENTER THE POSITION AT WHICH YOU WANT TO INSERT: ");
scanf(" %d",&pos);
insert(num,pos);
break;
}

case 2:
{
struct node *n;
printf("THE  ELEMENTS IN THE LIST ARE : ");
display(n);
break;
}

case 3:
{
int num;
printf("PLEASE ENTER A NUMBER FROM THE LIST :");
scanf("%d",&num);
delnode(num);
break;
}

case 4:
{
int num;
printf("ENTER THE VALUE TO BE INSERTED:");
scanf("%d",&num);
append(num);
break;
}

case 5:
{
int a,num;
printf("\n\tEnter value whose occurence has to be count \n:");
scanf("%d",&num);
a=count(num);       
printf("\n\tNumber of occurences of %d is %d",num,a);
break;
}
 
case 6:
{
int num,a;
printf("\n\tEnter value whose index has to be find:");
scanf("%d",&num);
a=index11(num);       
printf("\n\tThe first occurence of element is at position %d \n",a);
break;
}

case 7:
{
printf("Total number of elements in the list are %d ",length());
break;
}

case 8:
{
int num,n;
printf("Enter the size of list of number u want to append \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number \n");
scanf("%d",&num);
append(num);
}
break;
}
case 9:
{
reverse(p);
break;
}
	
case 0:
{
exit(0);
}
}
}
}

